
module spi_tx_buf(
    // APB interface
    input                               pclk_i,
    input                               prstn_i,
    input  [31:0]                       paddr_i,
    input                               penable_i,
    input                               psel_i,
    input  [2 :0]                       pprot_i,
    input  [31:0]                       pwdata_i,
    input                               pwrite_i,
    output [31:0]                       prdata_o,

    // spi fsm ctrl
    input                               txdata_state_i,
    input                               tx_1b_data_done_i,
    input                               byte_cnt_flag_i,
    output [7 :0]                       rdata_o                             
);

wire                                    apb_wen;
reg  [7:0]                              raddr_cnt;
assign apb_wen  = ((psel_i == 1'b1) && (penable_i == 1'b1) && (pwrite_i == 1'b1)) ? 1'b1 : 1'b0;
assign prdata_o = 'd0;

always@(posedge pclk_i or negedge prstn_i)begin
    if(!prstn_i)begin
        raddr_cnt <= 'd0;
    end
    else if(txdata_state_i & tx_1b_data_done_i & byte_cnt_flag_i)begin
        raddr_cnt <= 'd0;
    end
    else begin
        if(txdata_state_i)begin
            raddr_cnt <= tx_1b_data_done_i ? (raddr_cnt + 1'b1) : raddr_cnt;
        end
        else begin
            raddr_cnt <= 'd0;
        end
    end
end

tx_buf_4_8_64 u_tx_buf_4_8_64(
    .clk_i      (   pclk_i      ),
    .rstn_i     (   prstn_i     ),

    .wen_i      (   apb_wen     ),
    .waddr_i    (   paddr_i     ),
    .wdata_i    (   pwdata_i    ),

    .raddr_i    (   raddr_cnt   ),
    .rdata_o    (   rdata_o     )   
);
endmodule